<%@ page language="java" isELIgnored="false" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>字典信息管理</title>
     <%@include file="/common/include.jsp"%>
     	<script>
		$(function(){
			$('#dictList').treegrid({
				nowrap: false,
				rownumbers: true,
				animate:true,
				collapsible:true,
				pagination:true,
				url:'admin/dictManagerAction!dictListJSON.action',
				idField:'ids',
				treeField:'names',
				frozenColumns:[[
				    {field:'ck',checkbox:true},
	                {title:'字典名称',field:'names',width:200,
		                formatter:function(value){
		                	return '<span style="color:red">'+value+'</span>';
		                }
	                }
				]],
				columns:[[
					 {field:'orderIds',title:'排序',width:50,rowspan:2},
					 {field:'description',title:'描述说明',width:250,rowspan:2}
				]],
				onBeforeExpand:function(row,param){
					if(row){
						$(this).treegrid('options').url = 'admin/dictManagerAction!dictListJSON.action?dict.ids='+row.ids;
					}else{
						$(this).treegrid('options').url = 'admin/dictManagerAction!dictListJSON.action';
					}
				},
				toolbar:[{
					text:'新增',
					iconCls:'icon-add',
					handler:function(){
						append();
					}
				},'-',{
					text:'修改',
					iconCls:'icon-edit',
					handler:function(){modify();}
				},'-',{
					text:'上移',
					iconCls:'icon-edit',
					handler:function(){up();}
				},'-',{
					text:'删除',
					iconCls:'icon-remove',
					handler:function(){remove();}
				},'-',{
					text:'查询',
					iconCls:'icon-search',
					handler:function(){
						$('#dictList').treegrid('options').url = 'admin/dictManagerAction!dictListJSON.action?isSearch=true&_query.names='+$('#names').val();
						$('#dictList').treegrid("reload");
					}
				}],
				onLoadSuccess:function(){
					$('.datagrid-toolbar').append($('#searchTable'));
					$('#searchTable').show();
			       //分页工具条----start ----
				   	var dictListPager = $(this).datagrid('getPager');
					if (dictListPager){	
						$(dictListPager).pagination({
							pageList:[5,10,15],
							displayMsg:'第一级{from}-{to},共{total}',
							buttons:[{
								text:'取消选择',
								iconCls:'icon-unselectAll',
								handler:function(){
									 $('#dictList').treegrid('unselectAll');
								}
							}],
							onSelectPage:function(pageNumber, pageSize){
								$(this).pagination('loading');
								$('#dictList').treegrid('options').url = 'admin/dictManagerAction!dictListJSON.action?rows='+pageSize+'&&page='+pageNumber;
								$('#dictList').treegrid('reload');
								$(this).pagination('loaded');
						   }
						});
					};
					//分页工具条----end -----
				},
				onContextMenu: function(e,row){
					e.preventDefault();
					$(this).treegrid('unselectAll');
					$(this).treegrid('select', row.ids);
					$('#contextMenu').menu('show', {
						left: e.pageX,
						top: e.pageY
					});
				}
			});
		});
		function modify(){
			var node = $('#dictList').treegrid('getSelected');
			if (node){
				WinOpen(node.ids);
		    }else{
		    	$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>请选择一条数据！</b></div>','warning');
		    }
		}
		function append(){
		   var node = $('#dictList').treegrid('getSelected');
		   WinOpen();
		   if (node){
		   		$('#dictForm').attr('parentDictIds').value = node.ids;
		   }
		}
		function up(){
			var node = $('#dictList').treegrid('getSelected');
			if (node){
			   var pNode = $('#dictList').treegrid('getParent', node.ids);
			   if(pNode){
			   	    var TPNode = $('#dictList').treegrid('getParent',pNode.ids);
					var url="admin/dictManagerAction!moveDict.action";
					$.post(
							url,
							{"dict.ids":node.ids,"parentDictIds":TPNode?TPNode.ids:0},
							function(data){
								eval('data='+data);  
					        	var state = data.state;
					        	var retMsg = data.resultMsg;
					        	$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>'+retMsg+'</b></div>','info');
					        	if(state==true){
					    			if(!TPNode){
						    			$('#dictList').treegrid('options').url = 'admin/dictManagerAction!dictListJSON.action';
					    			}
					    			$('#dictList').treegrid('reload',TPNode?TPNode.ids:null);
					        	}
							}
					);    			
			   }else
				   $.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>根节点不能往上移动！</b></div>','warning');
				   
		    }else{
		    	$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>请选择一条数据！</b></div>','warning');
		    }
		}
		function remove(){
			var node = $('#dictList').treegrid('getSelected');
			if (node){
				var pNode = $('#dictList').treegrid('getParent', node.ids);//父节点
				$.messager.confirm('提示信息', '<div style="padding-top:15px;padding-left:5px"><b>确定删除？</b></div>', function(r){
					if (r){
						var url="admin/dictManagerAction!deleteDict.action";
						$.post(
								url,
								{"dict.ids":node.ids},
								function(data){
									eval('data='+data);  
						        	var state = data.state;
						        	var retMsg = data.resultMsg;
						        	if(state==true){
						        		$('#dictList').treegrid('remove', node.ids);
						    			if(!pNode)
						    				$('#dictList').treegrid('options').url = 'admin/dictManagerAction!dictListJSON.action';
						    			$('#dictList').treegrid('reload',pNode?pNode.ids:null);
						        	}
						        	$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>'+retMsg+'</b></div>','info');
								}
						)
					}
				});

			}else{
		    	$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>请选择一条数据！</b></div>','warning');
		    }
		}
		function save(){
			//添加和修改保存
			var pids = $('#dictForm').attr('parentDictIds');//父节点ID
			var idsVal = $('#dictForm').attr('ids').value;//节点ID
			if(!pids.value)pids.value=-1;//父节点的父节点
			var pidsVal= pids.value;
			$('#dictForm').form('submit',{
		        onSubmit: function(){     	
		        	return $('#dictForm').form('validate');
		        },
		        success:function(data){
		        	eval('data='+data);
		        	var state = data.state;
		        	var retMsg = data.resultMsg;
		        	$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>'+retMsg+'</b></div>','info');
		        	if(state==true){		        	
		        		if(!idsVal&&pidsVal!=-1){
			        		var newNode = [{
			    				ids: data.ids,
			    				names: data.names,
			    				codes: data.codes,
			    				orderIds: data.orderIds,
			    				description: data.description
			    			}];
			    			$('#dictList').treegrid('append', {
			    				parent: (pidsVal!=-1?pidsVal:null),
			    				data: newNode
			    			});
		        	    }
		    			if(pidsVal==-1)
		    				$('#dictList').treegrid('options').url = 'admin/dictManagerAction!dictListJSON.action';
		    		    $('#dictList').treegrid('reload',pidsVal!=-1?pidsVal:null);	    			
						$('#dictWindows').window('close');
			        	$('#dictForm').form('clear');
		        	}
		        }
			});
		}
		function WinOpen(ids){
			   $('#dictForm').form('clear');
			   $('#dictWindows').window('open');
			   $('#dictWindows').panel('setTitle','增加字典项');
			   if(ids){
				   $('#dictWindows').panel('setTitle','修改字典项');
				   $('#dictForm').form('load', 'admin/dictManagerAction!viewDict.action?dict.ids='+ids); 
			   }
		       $('#dictForm').show();
		}
		function WinClose(){
			$('#dictForm').form('clear');
			$('#dictWindows').window('close');
		}
	</script>
</head>
<body class="easyui-layout">
  <div id="dictWindows" class="easyui-window" closed="true" iconCls="icon-save" draggable="false" modal="true" resizable="false" maximizable
  ="false" minimizable
  ="false" title="添加字典项" style="width:520px;height:268px;padding:5px;background:#fafafa;">
	 <div region="center" border="false" style="padding:12px; background:#fff; border:1px solid #ccc;">
	 	<%@include file="dictInfo.jsp"%>
     </div>
     <div region="south" border="false" style="text-align:right;height:30px;line-height:30px;">
     	<a class="easyui-linkbutton" icon="icon-save" href="javascript:void(0)" onclick="save()">保存</a>
     	<a class="easyui-linkbutton" icon="icon-back" href="javascript:void(0);" onclick="WinClose()">返回</a>
     </div>
  </div>
  <div region="center" style="padding:5px;" border="false">
	<table id="dictList" fit="true"></table>
	<div id="contextMenu" class="easyui-menu" style="width:120px;">
		<div onclick="append()" iconCls="icon-add">增加下级字典项</div>
		<div onclick="up()" iconCls="icon-edit">上移字典项</div>
		<div onclick="modify()" iconCls="icon-edit">修改字典项</div>
		<div onclick="remove()" iconCls="icon-remove">移除字典项</div>
	</div>
   </div>
	<table id="searchTable" style="display:none" border="0" cellpadding="0">
		<tr>
			<td>
				<B><input type="text" id="names"/></B>
			</td>
			<td>
				<B>字典名称</B>
			</td>
		<tr>
	</table>
</body>
</html>